package xyz.scootaloo.oj.offer

import org.junit.Test
import java.util.*

/**
 * @author flutterdash@qq.com
 * @since 2021/4/10 15:11
 */
class P21 {

    private fun exchange(nums: IntArray): IntArray {
        val size = nums.size
        var left = 0
        var right = size - 1
        while (left < right) {
            while (left < size && nums[left] % 2 != 0)
                left++
            while (right > 0 && nums[right] % 2 == 0)
                right--
            if (left >= right)
                break
            swap(nums, left, right)
        }
        return nums
    }

    private fun swap(nums: IntArray, x: Int, y: Int) {
        val tmp = nums[x]
        nums[x] = nums[y]
        nums[y] = tmp
    }

    @Test
    fun test0() {
        val input1 = intArrayOf(1, 3, 5)
        println(exchange(input1).contentToString())
    }

}
